package sunstar.com.xq.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * Created by Administrator on 2017/3/6.
 */

abstract class LazyFragment : Fragment() {
    lateinit var thisView: View
    //可见性控制
    private var isFragmentVisible = false
    //首次加载
    var isFirstLoad: Boolean = false
    //控制是否准备成功，防止出现空指针
    private var isPrepare: Boolean = false

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        thisView = inflater!!.inflate(initViewid(), null)
        isPrepare = true
        isFirstLoad = true
        initViewCompelte()
        loadData()
        return thisView
    }

    private fun loadData() {
        if (isPrepare && isFirstLoad && isFragmentVisible) {
            initData()
            isFirstLoad = false
        } else {
            reLook()
        }
    }

    fun reLook() {}
    abstract fun initData()

    abstract fun initViewid(): Int
    abstract fun initViewCompelte()

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (userVisibleHint) {//可见
            isFragmentVisible = true
            loadData()
        } else {//不可见

        }
    }

    override fun onDestroyView() {
        super.onDestroy()
    }
}
